<template>
  <div class="login-wrapper">
    <div class="modal">
      <el-form :model="user" :rules="rules" ref="userFormRef">
        <div class="title">英雄联盟</div>
        <el-form-item prop="userName">
          <el-input v-model="user.userName" />
        </el-form-item>
        <el-form-item prop="userPwd">
          <el-input v-model="user.userPwd" />
        </el-form-item>
        <el-form-item>
          <el-button class="btn" type="primary" @click="login(userFormRef)"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { ref, reactive } from "vue";
import api from "../api";
// import { post } from "../utils/request";

let store = useStore();
let router = useRouter();
let user = reactive({ userName: "admin", userPwd: "123456" });
let rules = reactive({
  userName: [
    {
      required: true,
      message: "请输入用户名",
      trigger: "blur",
    },
  ],
  userPwd: [
    {
      required: true,
      message: "请输入密码",
      trigger: "blur",
    },
  ],
});
let userFormRef = ref(null);

const login = (formRef) => {
  formRef.validate((vaild) => {
    // 根据刚刚设置的rules去匹配，都满足返回true
    // 只要有一个不满足返回false

    // 发送请求
    // (async () => {
    //发送登录请求
    //有可能出现错误的地方
    if (vaild) {
      // 发送请求
      api.login(user).then((res) => {
        console.log(res);
        store.commit("saveUserInfo", res);
        router.push("/");
        // let token = res.token;
        // console.log(token);
        // sessionStorage.setItem("token", token);
      });
    }
  });
};
</script>

<style lang="scss" scoped>
.login-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100vh;
  background-color: #fff;
  .modal {
    width: 500px;
    padding: 50px;
    background-color: pink;
    box-shadow: 0px 0px 10px 3px #ddd;
    border-radius: 5px;
    .title {
      text-align: center;
      font-size: 50px;
      margin-bottom: 30px;
      line-height: 1.5;
    }
    .btn {
      width: 100%;
    }
  }
}
</style>
